package online.exam.controller;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import online.exam.pojo.SubjectBank;
import online.exam.pojo.TestPaper;
import online.exam.pojo.vo.DoTestPaperVO;
import online.exam.pojo.vo.TestPaperVO;
import online.exam.pojo.vo.TestSubjectsVO;
import online.exam.service.TestService;
import online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王景煜
 * created in 2019/11/19 19:55
 */

@RequestMapping(value = "/test")
@RestController
public class TestController {

    @Autowired
    private TestService testServiceImpl;

    @GetMapping("/queryAll/{page}/{limit}")
    public PageUtils<TestPaperVO> queryAll(@PathVariable Integer page, @PathVariable Integer limit,
                                           @RequestParam String clazz) {
        return testServiceImpl.queryAllTestPaper(page, limit, clazz);
    }

    @PostMapping("/addRandomTest")
    public int addRandomTest(@RequestParam String testPaperString) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, TestPaper.class);
        List<TestPaper> tpList = mapper.readValue(testPaperString, jt);
        String ids = tpList.get(0).getSubject();
        return testServiceImpl.addRandomTest(tpList, ids);
    }

    @PostMapping("/addCusTest")
    public int addTest(@RequestParam String dataString) throws IOException {
        String[] data = dataString.split("\\|\\|\\|");
        String subjectBank = data[0];
        String testBank = data[1];
        // 创建 jackson 对象
        ObjectMapper om = new ObjectMapper();
        // 使用 jackson 将 json 转为 List<SubjectBank>
        JavaType sbJt = om.getTypeFactory().constructParametricType(ArrayList.class, SubjectBank.class);
        List<SubjectBank> bankList = om.readValue(subjectBank, sbJt);
        JavaType tpJt = om.getTypeFactory().constructParametricType(ArrayList.class, TestPaper.class);
        List<TestPaper> tpList = om.readValue(testBank, tpJt);
        return testServiceImpl.addTestPaper(bankList, tpList);
    }

    @GetMapping("/queryAllClazz")
    public PageUtils<SubjectBank> queryAllClazz(@RequestParam Integer page, @RequestParam Integer limit,
                                                @RequestParam String subjectClazz) {
        return testServiceImpl.queryAllSubjectByPage(page, limit, subjectClazz);
    }

    @GetMapping("/queryTestPaperIsDone")
    public PageUtils<DoTestPaperVO> queryTestPaperIsDone(@RequestParam Integer page, @RequestParam Integer limit,
                                                         @RequestParam String stuId) {
        return testServiceImpl.queryTestPaperIsDone(page, limit, stuId);
    }

    @GetMapping("/querySubjectByTestId")
    public List<TestSubjectsVO> querySubjectByTestId(@RequestParam String testId) {
        return testServiceImpl.querySubjectByTestId(testId);
    }

}
